package com.winz.cultureapp.fragment;

import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;

import com.mine.xrecyclerview.XRecyclerView;
import com.mine.xrecyclerview.rcvadapter.OnItemClickListener;
import com.winz.cultureapp.R;
import com.winz.cultureapp.adapter.xrcv.PersonalItem3Adapter;
import com.winz.cultureapp.base.BaseFragment;
import com.winz.cultureapp.entity.PersonItem3ListBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 个人记录--志愿服务
 */
public class PersonalItem3Fragment extends BaseFragment implements XRecyclerView.LoadingListener, OnItemClickListener {
    @BindView(R.id.xrcv_data)
    public XRecyclerView mXrcv_data;

    private boolean isRefresh = true;
    private int pageSize = 10;
    private int currentPage = 1;
    private int totalPage = 0;
    private List<PersonItem3ListBean.DataBean.ListBean> mDatas;
    private PersonalItem3Adapter mAdapter;

    private String title;


    public static PersonalItem3Fragment getInstance(String title){
        PersonalItem3Fragment fragment = new PersonalItem3Fragment();
        Bundle bundle = new Bundle();
        bundle.putString("title",title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int contentView() {
        return R.layout.fragment_personal_item3;
    }

    @Override
    protected void initViews(View view) {
        mDatas = new ArrayList<>();
        // XrecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        mXrcv_data.setLayoutManager(manager);
        mXrcv_data.setPullRefreshEnabled(true);
        mXrcv_data.setLoadingMoreEnabled(false);
        mAdapter = new PersonalItem3Adapter(mContext,mDatas,R.layout.empty_view,R.layout.item_personal_item3);
    }

    @Override
    protected void initEvents() {

        mXrcv_data.setAdapter(mAdapter);
        mXrcv_data.setLoadingListener(this);
        mAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void loadDatas() {
        getList();
    }

    @Override
    protected void getDatas(Bundle arguments) {
        title = arguments.getString("title");
    }

    /**
     * 获取数据列表
     */
    private void getList() {
//        mDatas.add(new PersonItem3ListBean.DataBean.ListBean());
//        mDatas.add(new PersonItem3ListBean.DataBean.ListBean());
//        mDatas.add(new PersonItem3ListBean.DataBean.ListBean());
//        mDatas.add(new PersonItem3ListBean.DataBean.ListBean());
//        mDatas.add(new PersonItem3ListBean.DataBean.ListBean());
//        mDatas.add(new PersonItem3ListBean.DataBean.ListBean());

        mAdapter.setDatas(mDatas);
        if (isRefresh){
            mXrcv_data.refreshComplete();
        }else {
            mXrcv_data.loadMoreComplete();
        }
    }


    /**
     * 刷新
     */
    @Override
    public void onRefresh() {
        isRefresh = true;
        currentPage = 1;
        loadDatas();
    }

    /**
     * 加载
     */
    @Override
    public void onLoadMore() {
        isRefresh = false;
        if (currentPage>=totalPage){
            mXrcv_data.setNoMore(true,true,"没有更多数据了");
        }else {
            currentPage++;
            loadDatas();
        }
    }

    @Override
    public void onItemClick(ViewGroup parent, View view, int position) {
//        Bundle bundle = new Bundle();
//        bundle.putString(Constants.KEY.NEWS.ID,mDatas.get(position).id);
//        bundle.putString(Constants.KEY.NEWS.TOP_TITLE,title);
//        bundle.putString(Constants.KEY.NEWS.HREF,"https://mini.eastday.com/a/n190117072753205.html?qid=02012");
//        startActivity(NewsDesActivity.class,false,bundle);
    }

    @Override
    public boolean onItemLongClick(ViewGroup parent, View view, int position) {
        return false;
    }
}
